﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace FloodFree.Phone
{
    public partial class SensorsPage : PhoneApplicationPage
    {

        private ItemViewModel cityModel;

        public SensorsPage()
        {
            InitializeComponent();
            DataContext = App.SensorViewModel;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedIndex", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);
                cityModel = App.ViewModel.Items[index];
                App.SensorViewModel.CityViewModel = cityModel;
                App.SensorViewModel.LoadData(cityModel);
            }
        }

        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedIndex=" + MainListBox.SelectedIndex + "&cityName=" + cityModel.LineOne, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }
    }
}